package com.lagou.edu.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String  username=request.getParameter("username") ;
        String  password=request.getParameter("password") ;

        String loginpath=request.getRequestURI() ;

        if(loginpath.contains("/login")){
            if("admin".equals(username)&&"admin".equals(password)){
                request.getSession().setAttribute("username",username);
                return  true ;
            }else{
                request.setAttribute("message","用户名或密码错误，请重新输入");
                request.getRequestDispatcher("login.jsp").forward(request, response);
                return false ;
            }
        }else{
            String  u=(String)request.getSession().getAttribute("username") ;
            if(u==null || "".equals(u)){
                request.setAttribute("message","请登录");
                request.getRequestDispatcher("login.jsp").forward(request, response);
                return false ;
            }else{
                return  true ;
            }

        }







    }


}
